package com.example.view.module.scroll;

import android.database.ContentObserver;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.adapter.TabAdapter;
import com.example.common.base.AbsActivity;
import com.example.common.utils.DisplayUtils;
import com.example.view.R;
import com.example.view.databinding.ActMultiScrollBinding;
import com.example.view.module.scroll.fragment.PageFragment;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener;

import java.util.Arrays;

@Route(path ="/view/MultiScroll")
public class MultiScrollActivity extends AbsActivity<ActMultiScrollBinding> {

    private String[] mTitles;
    private Fragment[] fragments;
    private int mOffset;
    private int mScrollY;
    private int toolBarPositionY;

    @Override
    protected int getLayoutId() {
        return R.layout.act_multi_scroll;
    }

    @Override
    protected void main() {
        init();
    }

    private void init() {
        mTitles = new String[]{"tab1", "tab2", "tab3"};
        fragments = new Fragment[]{PageFragment.getInstance(), PageFragment.getInstance(), PageFragment.getInstance()};
        mBinding.llButtonBar.setAlpha(0);
        mBinding.toolbar.setBackgroundColor(0);
        TabAdapter adapter = new TabAdapter(getSupportFragmentManager(), 0);
        adapter.addInstance(Arrays.asList(mTitles), Arrays.asList(fragments));
        mBinding.viewPager.setOffscreenPageLimit(mTitles.length);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mBinding.tabLayoutVp.setupWithViewPager(mBinding.viewPager);
    }

    @Override
    protected void initListener() {
        ContentObserver mNavigationStatusObserver = new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);

            }
        };

        mBinding.refreshLayout.setOnMultiListener(new SimpleMultiListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
                mOffset = offset / 2;
                mBinding.ivHeader.setTranslationY(mOffset - mScrollY);
                mBinding.toolbar.setAlpha(1 - Math.min(percent, 1));
            }
        });

        mBinding.toolbar.post(new Runnable() {
            @Override
            public void run() {
                dealWithViewPager();
            }
        });

        mBinding.scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {

            int lastScrollY = 0;
            int h = DisplayUtils.dpToPx(170);
            int color = ContextCompat.getColor(getApplicationContext(), R.color.mainWhite) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                int[] location = new int[2];
                mBinding.tabLayoutVp.getLocationOnScreen(location);
                int yPosition = location[1];
                if (yPosition < toolBarPositionY) {
                    mBinding.tabLayout.setVisibility(View.VISIBLE);
                    mBinding.scrollView.setNeedScroll(false);
                } else {
                    mBinding.tabLayout.setVisibility(View.GONE);
                    mBinding.scrollView.setNeedScroll(true);
                }

                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
                    mBinding.llButtonBar.setAlpha(1f * mScrollY / h);
                    mBinding.toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
                    mBinding.ivHeader.setTranslationY(mOffset - mScrollY);
                }

                if (scrollY == 0) {
                    mBinding.ivBack.setImageResource(R.mipmap.icon_back_white);
                    mBinding.ivMenu.setImageResource(R.mipmap.icon_menu_white);
                } else {
                    mBinding.ivBack.setImageResource(R.mipmap.icon_back_black);
                    mBinding.ivMenu.setImageResource(R.mipmap.icon_menu_white);
                }
                lastScrollY = scrollY;
            }
        });


    }

    private void dealWithViewPager() {
        toolBarPositionY = mBinding.toolbar.getHeight();
        ViewGroup.LayoutParams params = mBinding.viewPager.getLayoutParams();
        params.height = DisplayUtils.getScreenHeightPx(getApplicationContext()) - toolBarPositionY - mBinding.tabLayoutVp.getHeight() + 1;
        mBinding.viewPager.setLayoutParams(params);
    }


}
